package com.hyphenate.easeui.utils.permssion;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

/**
 * Description:
 * Created by Andy Hong on 9/26/2017.
 */

class PermissionRequestImpl implements PermissionRequest {
    private Object mHost;
    private String[] mPermissions;

    PermissionRequestImpl(Object host) {
        this.mHost = host;
    }

    @Override
    public PermissionRequest request(String... permissions) {
        Guardian.checkPermissionIfOpenCheck(getContext(), permissions);
        mPermissions = permissions;
        return this;
    }

    @Override
    public void call(int requestCode) {
        if (mHost instanceof Activity) {
            ActivityCompat.requestPermissions((Activity) mHost, mPermissions, requestCode);
        } else if (mHost instanceof Fragment) {
            ((Fragment) mHost).requestPermissions(mPermissions, requestCode);
        } else if (mHost instanceof android.app.Fragment) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ((android.app.Fragment) mHost).requestPermissions(mPermissions, requestCode);
            }
        }

    }

    @Override
    public void call(OnRequestPermissionsResultListener listener) {
        Context context = getContext();
        if (context instanceof FragmentActivity) {
            FragmentActivity fragmentActivity = (FragmentActivity) context;
            FragmentManager supportManager = fragmentActivity.getSupportFragmentManager();
            CompatPermissionFragment supportFragment = (CompatPermissionFragment) supportManager.findFragmentByTag(Guardian.PERMISSION_FRAGMENT_NAME);
            if (supportFragment == null) {
                supportFragment = new CompatPermissionFragment();
                supportManager.beginTransaction()
                        .add(supportFragment, Guardian.PERMISSION_FRAGMENT_NAME)
                        .commitAllowingStateLoss();
            }
            supportFragment.request(mPermissions, listener);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Activity activity = (Activity) context;
            android.app.FragmentManager fragmentManager = activity.getFragmentManager();
            PermissionFragment permissionFragment = (PermissionFragment) fragmentManager.findFragmentByTag(Guardian.PERMISSION_FRAGMENT_NAME);
            if (permissionFragment == null) {
                permissionFragment = new PermissionFragment();
                fragmentManager.beginTransaction()
                        .add(permissionFragment, Guardian.PERMISSION_FRAGMENT_NAME)
                        .commitAllowingStateLoss();
            }
            permissionFragment.request(mPermissions, listener);
        } else {
            throw new IllegalStateException("current host need sdk version >= 23");
        }
    }

    private Context getContext() {
        Activity activity=null;
        if (mHost instanceof Fragment) {
            activity = ((Fragment) mHost).getActivity();
            if (activity == null) {
                throw new IllegalArgumentException("You cannot start a load on a fragment before it is attached");
            }
        } else if (mHost instanceof android.app.Fragment) {
            activity = ((android.app.Fragment) mHost).getActivity();
            if (activity == null) {
                throw new IllegalArgumentException("You cannot start a load on a fragment before it is attached");
            }
        }
        return activity != null ? activity : (Context) mHost;
    }

}
